import os
import shutil

from .nodes.doubao import DouBao
from .nodes.seed_edit import SeedEdit
from .nodes.keep_face import KeepFaceSwap
from .nodes.face_swap import DouBaoFaceSwap
from .nodes.face_swap_ai import DouBaoFaceSwapAI
from .nodes.pretty_face import PrettyFaceSwap
from .nodes.monochrome import Monochrome

NODE_CLASS_MAPPINGS = {
    "SeedEdit": SeedEdit,
    "DouBao": DouBao,
    "KeepFaceSwap": KeepFaceSwap,
    "DouBaoFaceSwap": DouBaoFaceSwap,
    "DouBaoFaceSwapAI": DouBaoFaceSwapAI,
    "PrettyFaceSwap": PrettyFaceSwap,
    "Monochrome": Monochrome,

}
NODE_DISPLAY_NAME_MAPPINGS = {
    "SeedEdit": "Load Seed Edit ",
    "DouBao": "Load DouBao",
    "KeepFaceSwap": "DouBao Keep Face",
    "DouBaoFaceSwap": "DouBao Swap Face",
    "DouBaoFaceSwapAI": "DouBao Swap Face AI",
    "PrettyFaceSwap": "DouBao Pretty Face",
    "Monochrome": "to Monochrome Image"
}
__all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS']


def delete_all_in_folder(folder_path):
    """
    删除指定文件夹下的所有子文件和子文件夹
    :param folder_path: 目标文件夹路径
    """
    if os.path.isdir(folder_path):
        for root, dirs, files in os.walk(folder_path, topdown=False):
            for file in files:
                file_path = os.path.join(root, file)
                os.remove(file_path)
            for dir_name in dirs:
                dir_path = os.path.join(root, dir_name)
                shutil.rmtree(dir_path)
    elif os.path.isfile(folder_path) and os.path.exists(folder_path):
        os.remove(folder_path)


try:
    from aiohttp import web
    from server import PromptServer
    from folder_paths import output_directory, input_directory, temp_directory, user_directory


    @PromptServer.instance.routes.get("/clear/tmp")
    async def clean_tmp_data(request):
        delete_all_in_folder(output_directory)
        delete_all_in_folder(input_directory)
        delete_all_in_folder(temp_directory)
        delete_all_in_folder(user_directory)
        return web.json_response("hello")


    @PromptServer.instance.routes.get("/clear/input")
    async def clean_tmp_data(request):
        delete_all_in_folder(input_directory)
        return web.json_response("hello")


    @PromptServer.instance.routes.get("/clear/output")
    async def clean_tmp_data(request):
        delete_all_in_folder(output_directory)
        delete_all_in_folder(user_directory)
        return web.json_response("hello")


    @PromptServer.instance.routes.get("/clear/temp")
    async def clean_tmp_data(request):
        delete_all_in_folder(temp_directory)
        return web.json_response("hello")


    @PromptServer.instance.routes.get("/clear/user")
    async def clean_tmp_data(request):
        delete_all_in_folder(user_directory)
        return web.json_response("hello")


    @PromptServer.instance.routes.get("/clear/input/{folder_name}")
    async def clean_sub_data(request):
        folder_name = request.match_info.get("folder_name", None)
        sub_input_directory = os.path.join(input_directory, folder_name)
        delete_all_in_folder(sub_input_directory)
        return web.json_response("hello")

except ImportError:
    pass
